﻿#region using
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using conan.Saas.Framework;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.AspNetCore.Http;
using dotNET.Utility.Cache;
using dotNET.Data;
using dotNET.Application.Infrastructure;
using dotNET.Application.App;
using System.Text;
using Microsoft.Extensions.Logging;
using NLog;
using NLog.Web.AspNetCore;
using Microsoft.AspNetCore;
using dotNET.Utility;
using NLog.Web;
using NLog.Extensions.Logging;
using Microsoft.AspNetCore.Authentication.Cookies;
using conan.Saas.Framework.Middlewares;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc.Authorization;
using System.Net;
using System.IO;
using Microsoft.AspNetCore.Mvc.Rendering;
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Mvc.ViewEngines;
using dotNET.Repository;
using Autofac.Extensions.DependencyInjection;
using Autofac;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Controllers;
#endregion
namespace conan.Saas
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }
        // This method gets called by the runtime. Use this method to add services to the container.
        public IContainer ApplicationContainer { get; private set; }
        public IServiceProvider ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
            services.AddSession(options =>
            {
                options.Cookie.HttpOnly = true;
            });
            services.AddMemoryCache();
            services.AddOptions();
            services.Configure<SiteConfig>(Configuration.GetSection("SiteConfig"));
            services.AddLogging();
            services.AddCloudscribePagination();
            services.AddResponseCompression();
            services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
            services.AddMvc(cfg =>
            {
                cfg.Filters.Add(typeof(ExceptionAttribute));//异常捕获
            });
            services.AddMvc(cfg =>
            {
                cfg.Filters.Add(typeof(MvcMenuFilter));
            });
            services.AddScoped<IDbContext, DbContext>(serviceProvider => new DbContext(Configuration["Data:MyCat:ConnectionString"]));
            //services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
            //services.AddSingleton(typeof(ICacheService), new RedisCacheService());
            //services.AddTransient<IItemsDataApp, ItemsDataApp>();
            //services.AddTransient<IDepartmentApp, DepartmentApp>();
            //services.AddTransient<IUserApp, UserApp>();
            //services.AddTransient<IModuleApp, ModuleApp>();
            //services.AddTransient<IModuleButtonApp, ModuleButtonApp>();
            //services.AddTransient<IAreaListApp, AreaListApp>();
            //services.AddTransient<IRoleAuthorizeApp, RoleAuthorizeApp>();
            //services.AddTransient<IRoleApp, RoleApp>();
            //services.AddTransient<IOperateLogApp, OperateLogApp>();
            //services.AddTransient<ILoginLogApp, LoginLogApp>();
            //services.Replace(ServiceDescriptor.Transient<IControllerActivator, ServiceBasedControllerActivator>());
            //services.AddMvc();
            //var builder = new ContainerBuilder();//实例化 AutoFac  容器        
            //builder.RegisterModule(new Evolution());
            //builder.Populate(services);
            //ApplicationContainer = builder.Build();
            //return new AutofacServiceProvider(ApplicationContainer);//第三方IOC接管 core内置DI容器
            //使用AutoFac进行注入
            return new AutofacServiceProvider(AutofacExt.InitAutofac(services));
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, ICompositeViewEngine engine)
        {
            app.UseAuthentication();
            Encoding.RegisterProvider(CodePagesEncodingProvider.Instance);//这是为了防止中文乱码
            loggerFactory.AddNLog();//添加NLog
            env.ConfigureNLog("nlog.config");//读取Nlog配置文件
            app.UseDefaultImage(defaultImagePath: Configuration.GetSection("defaultImagePath").Value);
            app.UseResponseCompression();
            loggerFactory.AddConsole(Configuration.GetSection("Logging"));
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
            }
            app.UseStaticFiles();
            //页面的执行时间
            //app.UseExecuteTime();
            app.UseSession();
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                name: "cloudscribeWebPagination",
                template: "pager/{page?}"
                , defaults: new { controller = "Paging", action = "Index" }
            );
                routes.MapRoute("areaRoute", "{area:exists}/{controller}/{action=Index}/{id?}");
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
            });
        }
    }
}